package com.longjianghuasheng.logistics.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import com.longjianghuasheng.logistics.R
import com.longjianghuasheng.logistics.ext.*
import com.longjianghuasheng.logistics.weight.GradationScrollView
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.xuexiang.xui.widget.statelayout.StatefulLayout
import me.hgj.jetpackmvvm.state.ResultState

//没有头部的多状态布局
abstract class BaseMultiStateFragment<VM : BaseMultiStateViewModel, DB : ViewDataBinding> :
    BaseFragment<VM, DB>(), Refresh {

    protected lateinit var smartLayout: SmartRefreshLayout


    protected lateinit var mStatefulLayout: StatefulLayout

    protected lateinit var scrollView: GradationScrollView
    protected val clickListener = { view: View ->

        mViewModel.page = 1
        lazyLoadData()
    }

    //该类绑定的ViewDataBinding
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {


        return inflater.inflate(R.layout.fragment_base_multi_state, container, false).run {
            mStatefulLayout = findViewById(R.id.ll_stateful)
            smartLayout = findViewById(R.id.smartLayout)
            scrollView = findViewById(R.id.scroll_view)
            mDatabind = DataBindingUtil.inflate(inflater, layoutId(), container, false)
            mDatabind.lifecycleOwner = this@BaseMultiStateFragment
            scrollView.addView(mDatabind.root)
            this
        }
    }

    override fun initView(savedInstanceState: Bundle?) {

        smartLayout.run {
            setEnablePureScrollMode(!isRefresh())//是否启用纯滚动模式
            setEnableNestedScroll(!isRefresh())//是否启用嵌套滚动
            setEnableOverScrollBounce(!isRefresh())//是否启用越界回弹
            setEnableOverScrollDrag(true)//是否启用越界拖动（仿苹果效果）1.0.4

            setEnableLoadMore(isLoadMore())

            setOnRefreshListener {
                onRefresh()
            }
            setOnLoadMoreListener {
                onLoadMore()
            }

        }


    }

    override fun createObserver() {

        mViewModel.multiStateLiveData.observer(this) {
//            if (lifecycle.currentState != Lifecycle.State.RESUMED) return@observer

            if (it is ResultState.Loading && (smartLayout.isRefreshing || smartLayout.isLoading)) return@observer

            if (it is ResultState.Error && smartLayout.isLoading) {

                mViewModel.page--
                smartLayout.finishLoadMore(false)
                return@observer
            }

            when (it) {
                is ResultState.Loading -> mStatefulLayout.showLoading("")
                is ResultState.Success -> mStatefulLayout.showContent()
                is ResultState.Error -> mStatefulLayout.showError(it.error.errorMsg, clickListener)
                else -> mStatefulLayout.showEmpty()
            }

            smartLayout.finishRefresh()
            smartLayout.finishLoadMore()

        }
    }


    override fun onRefresh() {
        mViewModel.page = 1
        lazyLoadData()
    }

    override fun onLoadMore() {

        mViewModel.page++
        lazyLoadData()
    }

}